layui.use('form', function(){
	var form = layui.form;

	init();
	
	form.render();
});

function init(){
	$.ajax({
		type: 'post',
		url: '/base/article/getAllType',
		dataType: 'json',
		data: {'id': $("#id").val()},
		success: function(result){
			var code = result.code;
			var html = '';
			if(code==0){
				var data = result.data;
				var articleTypeInfo = result.articleTypeInfo;//当前文章选中的类型
				for(var i=0;i<data.length;i++){
					var map = data[i];
					var id = map.id;
					var type = map.articleType;
					//选中操作
					var selectHtml = '';
					if(articleTypeInfo!=null && articleTypeInfo!=undefined && articleTypeInfo.articleTypeId==id){
						selectHtml = ' selected ';
					}
					html += '<option value="' + id + '" ' + selectHtml + '>' + type + '</option>';
				}
				$("#articleType").append(html);
				layui.form.render();
			}
		},
		error: function(e){
			layui.layer.msg('网络异常', {icon: 5});
		}
	});	
}

//保存
function save(){
	var id = $("#id").val();
	var articleType = $("#articleType").val();
	var json = {
		"id": id,
		"articleType": articleType
	};
	$.ajax({
		type: 'post',
		url: '/base/article/saveType',
		dataType: 'json',
		data: json,
		success: function(result){
			var code = result.code;
			if(code==0){
				layui.layer.msg('保存成功', {icon: 6},function(){
					toBack();//刷新表格
				});
			}else{
				layui.layer.msg('保存失败', {icon: 5});
			}
		},
		error: function(e){
			layui.layer.msg('网络异常', {icon: 5});
		}
	});
}

//返回
function toBack(){
	var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
	parent.layer.close(index); //再执行关闭  
}